package main

import (
	"fmt"
	"gee"
	"log"
	"net/http"
	"time"
)

func onlyForV2() gee.HandlerFunc {
	return func(c *gee.Context) {
		// Start timer
		t := time.Now()
		// if a server error occurred
		//c.Fail(500, "Internal Server Error")
		// Calculate resolution time
		log.Printf("[%d] %s in %v for group v2", c.StatusCode, c.Req.RequestURI, time.Since(t))
	}
}

func main() {
	r := gee.Default()

	r.Static("/code", "./")

	r.GET("/", func(c *gee.Context) {
		c.String(http.StatusOK, "Hello word")
	})
	r.GET("/static/*file", func(c *gee.Context) {
		c.String(http.StatusOK, c.Params["file"])
	})
	r.GET("/hello/:name", func(c *gee.Context) {
		c.HTML(http.StatusOK, fmt.Sprintf("Hello, %s", c.Params["name"]))
	})

	g1 := r.Group("/test")
	g1.Use(gee.Logger())
	g1.GET("/headers", func(c *gee.Context) {
		c.JSON(http.StatusOK, c.Req.Header)
	})

	// index out of range for testing Recovery()
	r.GET("/panic", func(c *gee.Context) {
		names := []string{"be careful!"}
		c.String(http.StatusOK, names[100])
	})

	v2 := r.Group("/v2")
	v2.Use(onlyForV2()) // v2 group middleware
	{
		v2.GET("/hello/:name", func(c *gee.Context) {
			// expect /hello/geektutu
			c.String(http.StatusOK, "hello %s, you're at %s\n", c.Params["name"], c.Path)
		})
	}

	log.Fatal(r.Run(":8092"))
}
